package com.hpin.fansea.sale;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.GoodsDetailBean;
import com.hpin.fansea.utils.AbDateUtil;
import com.hpin.fansea.utils.CommonUtils;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.hpin.fansea.widget.PreviewPhotoActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.Serializable;

/**
 * Created by Aaron on 15/10/22.
 */
public class GoodsDetailActivity extends BaseActivity {

    private ImageView purchase_guanfang_btn_buy;
    private TextView purchase_guanfang_commentNum;
    private RelativeLayout purchase_guanfang_rlComment;
    private TextView purchase_guanfang_name;
    private TextView purchase_guanfang_title;
    private TextView purchase_guanfang_TgPr;
    private TextView purchase_guanfang_original_price;
    private TextView tv_yuan2;
    private TextView purchase_guanfang_sold;
    private TextView purchase_guanfang_xuzhi;

    private RatingBar purchase_guanfang_ratingBar;
    private TextView purchase_guanfang_grade;
    private TextView guanfang_detail_time;
    private TextView tv_sold;// 已售数量
    private ImageView purchase_guanfang_photo;
    private RelativeLayout scan_rl_describe;
    private Intent mIntent;
    private TextView tv_left_num;
    private RelativeLayout rl_text_photos;
    private String groupBuyId;
    private GoodsDetailBean.GoodsResult goodsResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_goods_detail);

        groupBuyId = getIntent().getStringExtra("groupBuyId");

        initFindViewById();

//        String moduleId = getIntent().getStringExtra("moduleId");

        getDetailData();

    }

    /**
     * 获取团购信息
     */
    private void getDetailData() {

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.GOODSDETAIL + "/" + groupBuyId
                + "?token=" + sp.getString(Constant.token, "") + "&systemType=" + Constant.SYSTEM_TYPE, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    GoodsDetailBean goodsDetailBean = JSONObject.parseObject(str, GoodsDetailBean.class);

                    goodsResult = goodsDetailBean.result;

                    if (goodsResult != null)
                        setData(goodsResult);

                } catch (Exception e) {
                    e.printStackTrace();
                    showToast(Constant.NODATA);
                }

            }
        }, null);

    }

    private void initFindViewById() {

        // 查看图文详情
        scan_rl_describe = (RelativeLayout) findViewById(R.id.scan_rl_describe);
        // 返回按钮
        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("团购详情");

        iv_left.setOnClickListener(this);
        // 封面图片
        purchase_guanfang_photo = (ImageView) findViewById(R.id.purchase_guanfang_photo);
        // 评分条
        purchase_guanfang_ratingBar = (RatingBar) findViewById(R.id.purchase_guanfang_ratingBar);
        purchase_guanfang_ratingBar.setVisibility(View.GONE);
        // 评论的布局
        purchase_guanfang_rlComment = (RelativeLayout) findViewById(R.id.purchase_guanfang_rlComment);
        //点击查看详情
        rl_text_photos = (RelativeLayout) findViewById(R.id.rl_text_photos);

        tv_left_num = (TextView) findViewById(R.id.tv_left_num);

        // 评论数
        purchase_guanfang_commentNum = (TextView) findViewById(R.id.purchase_guanfang_commentNum);
        // 立即抢购
        purchase_guanfang_btn_buy = (ImageView) findViewById(R.id.purchase_guanfang_btn_buy);
        // 团购名称
        purchase_guanfang_name = (TextView) findViewById(R.id.purchase_guanfang_name);
        // 评分数
        purchase_guanfang_grade = (TextView) findViewById(R.id.purchase_guanfang_grade);
        purchase_guanfang_grade.setVisibility(View.GONE);
        // 待售数量
        tv_sold = (TextView) findViewById(R.id.tv_sold);
        // 标题
        purchase_guanfang_title = (TextView) findViewById(R.id.purchase_guanfang_title);
        // 团购价格
        purchase_guanfang_TgPr = (TextView) findViewById(R.id.purchase_guanfang_TgPr);
        // 原价
        purchase_guanfang_original_price = (TextView) findViewById(R.id.purchase_guanfang_original_price);
        tv_yuan2 = (TextView) findViewById(R.id.tv_yuan2);
        // 待售数量
        purchase_guanfang_sold = (TextView) findViewById(R.id.purchase_guanfang_sold);
        // 剩余时间
        guanfang_detail_time = (TextView) findViewById(R.id.guanfang_detail_time);
        // 须知
        purchase_guanfang_xuzhi = (TextView) findViewById(R.id.purchase_guanfang_xuzhi);

        purchase_guanfang_photo.setOnClickListener(this);
        purchase_guanfang_rlComment.setOnClickListener(this);
        purchase_guanfang_btn_buy.setOnClickListener(this);

        scan_rl_describe.setOnClickListener(this);
    }

    private void setData(GoodsDetailBean.GoodsResult goods) {
        // 已售数量
        tv_sold.setText("已售:" + goods.sold);

        // 标题
        if (!TextUtils.isEmpty(goods.name)) {
            purchase_guanfang_name.setText(goods.name);
        } else {
            purchase_guanfang_name.setText("");
        }
        // 二级标题
        if (!TextUtils.isEmpty(goods.title)) {
            purchase_guanfang_title.setText(goods.title);
        } else {
            purchase_guanfang_title.setText("");
        }
        // 图片
        if (!TextUtils.isEmpty(goods.fmpic)) {

            ImageLoader.getInstance().displayImage(goods.fmpic, purchase_guanfang_photo);

        } else {
            purchase_guanfang_photo.setVisibility(View.GONE);
        }
        // 团购价格
        if (!TextUtils.isEmpty(goods.tgPr)) {
            purchase_guanfang_TgPr.setText(goods.tgPr);
        } else {
            purchase_guanfang_TgPr.setText("");
        }
        // 销售价格
        if (!TextUtils.isEmpty(goods.salePr)) {
            purchase_guanfang_original_price.getPaint().setFlags(
                    Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰
            purchase_guanfang_original_price.setText(goods.salePr);
        } else {
            purchase_guanfang_original_price.setText("");
            tv_yuan2.setText("");
        }
        purchase_guanfang_xuzhi.setText(goods.xuzhi);
        // 待售
        if (!TextUtils.isEmpty(goods.sold)) {
            purchase_guanfang_sold.setText("待售" + goods.sold);
        } else {
            purchase_guanfang_sold.setText("");
        }
        // 评分数
        if (!TextUtils.isEmpty(goods.avgScore)) {
            double avgSocre = CommonUtils.float0(Double.parseDouble(goods.avgScore));
            purchase_guanfang_ratingBar.setRating((float) avgSocre);
            purchase_guanfang_grade.setText(avgSocre + "");
        } else {
            purchase_guanfang_ratingBar.setRating(0);
            purchase_guanfang_grade.setText("");
        }
        // 评论数
        if (!TextUtils.isEmpty(goods.reviewNum)) {
            purchase_guanfang_commentNum.setText(goods.reviewNum + "人已评论");
        } else {
            purchase_guanfang_commentNum.setText("0人已评论");
        }
        // 剩余时间
        if (!TextUtils.isEmpty(goods.startTime)
                && !TextUtils.isEmpty(goods.endTime)) {
            String differenceDay = AbDateUtil.getDifferenceDay(goods.endTime, goods.startTime);

            guanfang_detail_time.setText(differenceDay);
        } else {
            guanfang_detail_time.setText("");
        }

        if (goods.photos != null) {

            int size = goods.photos.size();

            if (size > 0) {
                tv_left_num.setText(size + ")");
            } else {
                rl_text_photos.setVisibility(View.GONE);
            }
        } else {
            rl_text_photos.setVisibility(View.GONE);
        }

    }


    @Override
    public void widgetClick(View v) {


        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.purchase_guanfang_rlComment:

                // 游客权限控制
                if (Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
                    remindLogin("亲,登录后才能评论哦");
                    return;
                }
                mIntent = new Intent(this, PurchaseCommentActivity.class);
                if (goodsResult != null)
                    mIntent.putExtra("purchaseId", goodsResult.id_);
                startActivity(mIntent);

                break;

            case R.id.purchase_guanfang_btn_buy:
                // 游客权限控制
                if (Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
                    remindLogin("亲,登录后才能购买哦");
                    return;
                }
                mIntent = new Intent(this, PurchasePayAcrivity.class);

                mIntent.putExtra("goodsDetal", (Serializable) goodsResult);
                startActivity(mIntent);

                break;

            case R.id.purchase_guanfang_photo:

                // 点击查看大图
                mIntent = new Intent(this, PreviewPhotoActivity.class);
                if (goodsResult != null)
                    mIntent.putExtra("picLists", (Serializable) goodsResult.photos);
                startActivity(mIntent);
                break;

            case R.id.scan_rl_describe:

                // 点击查看图文详情
                mIntent = new Intent(this, PurchaseDescribeActivity.class);

                if (goodsResult != null)
                    mIntent.putExtra("describe", goodsResult.description_);

                startActivity(mIntent);

                break;
            default:
                break;

        }

    }
}
